package com.ordersystem.demo.controller;

import com.ordersystem.demo.model.ProductCategory;
import com.ordersystem.demo.model.ProductInfo;
import com.ordersystem.demo.service.ProductCatagoryService;
import com.ordersystem.demo.service.ProductInfoService;
import com.ordersystem.demo.vo.ProductCategoryVO;
import com.ordersystem.demo.vo.JasonVo;
import com.ordersystem.demo.vo.ProductInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductCatagoryService catagoryService;

    @Autowired
    private ProductInfoService productInfoService;

    @GetMapping("/list")
    public JasonVo productList(){
        JasonVo jasonVo = new JasonVo();
        jasonVo.setCode(0);
        jasonVo.setMsg("成功");
        List<ProductCategoryVO> productCategoryVOS = new ArrayList<>();
        List<ProductCategory> productCategoryList =catagoryService.findAll();
        List<ProductInfo> productInfos= productInfoService.findAll();
        for(ProductCategory productCategory:productCategoryList){
            ProductCategoryVO productCategoryVO = new ProductCategoryVO();
            productCategoryVO.setCategoryName(productCategory.getCategoryName());
            productCategoryVO.setCategoryType(productCategory.getCategoryType());
            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for(ProductInfo productInfo:productInfos){
                if(productCategory.getCategoryType().equals(productInfo.getCategoryType())){
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    productInfoVO.setProductId(productInfo.getProductId());
                    productInfoVO.setProductName(productInfo.getProductName());
                    productInfoVO.setProductPrice(productInfo.getProductPrice());
                    productInfoVO.setProductDescription(productInfo.getProductDescription());
                    productInfoVO.setProductIcon(productInfo.getProductIcon());
                    productInfoVOList.add(productInfoVO);
                }
            }
            productCategoryVO.setFoods(productInfoVOList);
            productCategoryVOS.add(productCategoryVO);
        }
        jasonVo.setData(productCategoryVOS);

        return jasonVo;
    }
}
